package com.huiminpay.merchant.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.common.cache.util.RandomUuidUtil;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.convert.MerchantDetailVoConvert;
import com.huiminpay.merchant.convert.MerchantVoConvert;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.service.SmsService;
import com.huiminpay.merchant.vo.MerchantDetailVO;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: yz
 * @Date: 2024/7/17 19:31
 */
@RestController
@Api(value = "商户应用平台",tags = "商户平台应用接口",description = "商户平台应用接口2")
@Slf4j
public class MerchantController {
    @Reference //注入的远程调用的接口
    MerchantService merchantService;

    @Autowired  //注入的本地接口 将本地的bean注入
    SmsService smsService;

    @ApiOperation(value = "根据商户id查询商户信息")
    @GetMapping("/merchants/{id}")
    @ApiImplicitParam(name = "id",value = "商户id",required = true,dataType = "Long",paramType = "path")
    public MerchantDTO getMerchantById(@PathVariable("id") Long id){
        MerchantDTO merchantDTO = merchantService.MerchantById(id);
        return merchantDTO;
    }

    @ApiOperation("获取登录用户的商户信息")
    @GetMapping(value="/my/merchants")
    public MerchantDTO getMyMerchantInfo(){
        // 从token 中获取商户id
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchant = merchantService.MerchantById(merchantId);
        return merchant;
    }

    @ApiOperation(value = "获取手机验证码")
    @GetMapping("/sms")
    @ApiImplicitParam(name = "phone",value = "手机号",required = true,dataType = "String",paramType = "query")
    public String getSMSCode(@RequestParam("phone") String phone){
        //向验证码服务发送验证码
        log.info("向手机号:{}发送验证码", phone);
        return smsService.sendSms(phone);
    }
    //商户注册
    //----商户注册
    @ApiOperation("注册商户")
    @ApiImplicitParam(name = "merchantRegister", value = "注册信息", required = true, dataType =
            "MerchantRegisterVo", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVo registerMerchant(@RequestBody MerchantRegisterVo merchantRegister) {

        //合法性判断
        if (merchantRegister==null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        //手机号非空校验
        if(StringUtil.isBlank(merchantRegister.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        //校验手机号的合法性
        if (!PhoneUtil.isMatches(merchantRegister.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100109);
        }

        //用户名空判断
        if (StringUtil.isBlank(merchantRegister.getUsername())){
            throw new BusinessException(CommonErrorCode.E_100110);
        }
        //密码非空校验
        if (StringUtil.isBlank(merchantRegister.getPassword())){
            throw new BusinessException(CommonErrorCode.E_100111);
        }
        //验证码非空校验
        if (StringUtil.isBlank(merchantRegister.getVerifiyCode()) || StringUtil.isBlank(merchantRegister.getVerifiykey())){
            throw new BusinessException(CommonErrorCode.E_100103);
        }


        //    1.验证 验证码
        smsService.checkSmsCode(merchantRegister.getVerifiykey(),merchantRegister.getVerifiyCode());

        //    2.商户注册
        //    vo转dto
        MerchantDTO merchantDTO = MerchantVoConvert.INTSTANCE.MerchantRegisterVo2Dto(merchantRegister);
        /*MerchantDTO merchantDTO = new MerchantDTO();
        merchantDTO.setMobile(merchantRegister.getMobile());
        merchantDTO.setUsername(merchantRegister.getUsername());
        merchantDTO.setPassword(merchantRegister.getPassword());*/

        MerchantDTO merchant = merchantService.createMerchant(merchantDTO);
        return merchantRegister;
    }
    @Autowired
    OSS oss;
    @Value("${oss.aliyun.endpoint}")
    private String endpoint;
    @Value("${oss.aliyun.bucket}")
    private String bucket;
    @Value("${oss.aliyun.domain}")
    private String domain;

    @ApiOperation("文件上传")
    @PostMapping("upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        String originalFilename = file.getOriginalFilename();//源文件名称
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件后缀 .txt .png
        String fileName = RandomUuidUtil.getUUID() + "_" + extName;//文件名称
        try {
            //构建请求对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, fileName, file.getInputStream());
            //文件上传
            oss.putObject(putObjectRequest);
            return domain+ fileName;//返回资源的有效标识
        } catch (IOException e) {
            throw new BusinessException(CommonErrorCode.E_100106); //上传失败
        }
    }

    @ApiOperation("资质申请")
    @PostMapping("/my/merchants/save")
    @ApiImplicitParam(name="merchantInfo",value="商户认证资料",required = true,dataType = "MerchantDetailVO",paramType = "body")
    public void saveMerchantInfo(@RequestBody MerchantDetailVO merchantInfo){
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchantDTO = MerchantDetailVoConvert.INTSTANCE.MerchantDetailVo2Dto(merchantInfo);
        merchantService.applyMerchant(merchantId, merchantDTO);
    }





    //测试
    @ApiOperation(value = "测试")
    @GetMapping("/hello")
    @ApiImplicitParam(name = "msg",value = "输入的信息",required = true,dataType = "String",paramType = "query")
    public String hello(String msg){
        return "hello "+msg;
    }

    @ApiOperation(value = "测试数据2")
    @PostMapping("/hello2")
    @ApiImplicitParam(name = "msg",value = "测试信息",required = true,dataType = "String",paramType = "query")
    public String hello2(String msg){
        return "hello "+msg;
    }
}
